<template>
  <!-- 授权用户 -->
  <el-dialog title="选择师傅" :visible.sync="visible" width="800px" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="" prop="realName">
        <el-input
            v-model="queryParams.realName"
            placeholder="师傅姓名"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="mobileNo">
        <el-input style="width: 150px"
            v-model="queryParams.mobileNo"
            placeholder="手机号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="" prop="projName">
        <el-select v-model="queryParams.projCode" placeholder="服务类型" clearable style="width: 120px">
          <el-option v-for="(item ,index) in projList" :key="item.projCode" :label="item.projName" :value="item.projCode"/>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="itemCode">
        <el-cascader
            v-model="cateItemValues"
            :options="cateOptions"
            @change="handleChangeCate" clearable filterable  placeholder="服务产品"></el-cascader>
      </el-form-item>

      <el-form-item label="" prop="workAreaDistrictCode">
        <el-cascader
            v-model="districtValues"
            :options="districtOptions"
            @change="handleChangeDistrict" clearable filterable  placeholder="服务区域"></el-cascader>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table ref="table" :data="workerList" @select-all="handleAllSelect" @select="handleSelect" @selection-change="handleSelectionChange" height="260px"><!--@row-click="clickRow"-->
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="序号" width="50px" align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="师傅姓名" prop="realName" :show-overflow-tooltip="true" />
        <el-table-column label="手机号" align="center" prop="mobileNo" :show-overflow-tooltip="true"/>
        <el-table-column label="证件照" align="center" prop="idCardFront" >
          <template slot-scope="scope">
            <el-image
                style="width: 50px; height: 50px; margin-bottom:-4px"
                :src="scope.row.idCardFront"
                :preview-src-list="[scope.row.idCardFront]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="地址" align="center" prop="address" :show-overflow-tooltip="true"/>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectWorker" :loading="submitLoading">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {listWorker} from "@/api/worker/worker";
import {listProjAct} from "@/api/cms/prod/proj";
import {cateOptionsSelect, getCateOptionselect} from "@/api/cms/prod/cate";
import {districtOptionSelect} from "@/api/cms/district/district";
import {assignWorker} from "@/api/wkorder/wkorder";
export default {
  props: {
    // 业务编号 由父组件传递过来
    bizId: {
      type: [Number, String]
    }
  },
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      selectWorkerId: undefined,
      // 总条数
      total: 0,
      // 用户数据
      workerList: [],
      projList:"",
      cateOptions:[],
      cateItemValues:[],
      districtOptions:[],
      districtValues:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        auditStatus:"1",
        acctStatus:"1",
        realNameComp:"like",
        realName: undefined,
        mobileNoComp:"like",
        mobileNo:undefined,
        projName:undefined,
        itemCode:undefined,
        projCode:undefined,
        workAreaDistrictCode:undefined,
        bizId:undefined
      },
      submitLoading:false,
    };
  },
  methods: {
    // 显示弹框
    show() {
      this.queryParams.bizId=this.bizId;

      this.getList();
      this.getProjList();
      this.getCateOptions();
      this.getDistrictOptions();

      this.visible = true;
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let selObj = selection.map(item => item.realName);

    },
    handleSelect(selection, row) {
      this.selectWorkerId=undefined;
      // 清除 所有勾选项
      this.$refs.table.clearSelection()
      // 当表格数据都没有被勾选的时候 就返回
      // 主要用于将当前勾选的表格状态清除
      if(selection.length == 0) return
      this.selectWorkerId=row.pkId
      this.$refs.table.toggleRowSelection(row, true);
    },
    handleAllSelect(){
      this.$refs.table.clearSelection()
    },
    // 查询表数据
    getList() {
      listWorker(this.queryParams).then(response => {
        this.workerList = response.data;
        this.total = Number(response.total);
      });
    },
    /**
     * 查询服务项目
     */
    getProjList(){
      listProjAct().then(response =>{
        this.projList=response.data;
      });
    },
    /**
     * 产品类别的选择内容
     */
    getCateOptions(){
      cateOptionsSelect().then(data => {
        this.cateOptions = data
      });
    },
    getDistrictOptions(){
      districtOptionSelect().then( response => {
        this.districtOptions = response;
      });
    },
    handleChangeCate(value){
      this.cateItemValues=value;
      if(value){
        this.queryParams.itemCode=value[1];
      }
    },
    handleChangeDistrict(value){
      this.districtValues=value;
      if(value.length==3){
        this.queryParams.workAreaDistrictCode=value[2];
      }else{//无区的情况
        this.queryParams.workAreaDistrictCode=value[1];
      }
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 选择授权用户操作 */
    handleSelectWorker() {
      const bizId = this.queryParams.bizId;//this.bizId;

      const workerId = this.selectWorkerId;
      if (!workerId) {
        this.$modal.msgError("请选择要分配的师傅");
        return;
      }
      let data={
        orderId: bizId,
        workerId: workerId
      }
      console.log(data)
      this.submitLoading=true;
      assignWorker(data).then(res => {
        this.$modal.msgSuccess(res.data);
        if (res.code === 200) {
          this.visible = false;
          this.$emit("ok");
        }
      }).finally(() =>{
        this.submitLoading=false;
      });
    }
  }
};
</script>
